package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LC77 {
    // Leetcode 77. Combinations
    // Backtracking
    // Time Complexity:O(C(n,k))
    // Space Complexity:O(k)
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        backtracking(n, k, result, 1, new ArrayList<>());
        return result;
    }

    private void backtracking(int n, int k, List<List<Integer>> result, int begin, ArrayList<Integer> list) {
        if (list.size() == k) {
            result.add(new ArrayList<>(list));
            return;
        }

        for (int i = begin; i <= n; i++ ) {
            list.add(i);
            backtracking(n, k, result, i+1, list);
            list.remove(list.size()-1);
        }
    }

    public static void main(String[] args) {
        for (List<Integer> list : new LC77().combine(4, 2)) {
            for (Integer integer : list) {
                System.out.print(integer + ",");
            }
            System.out.println();
        }
    }
}
